<template>
	<view>

		<form @submit="formSubmit">
			<view class="pageContainer">

				<view class="worksiteInfor_box">
					<view class="worksiteName">工地名称：{{worksiteDetail.name}}</view>
					<view class="foremanName">带班长：{{worksiteDetail.userInfo.name}}</view>
				</view>

				<view class="form_box">
					<view class="formCon">
						<view class="formTips">
							补充资料，方便带班识别并审核
						</view>
						<view class="formItem">
							<view class="label"><text>*</text>姓名</view>
							<view class="controls">
								<input class="input" type="text" name="name" placeholder='请输入姓名'
									placeholder-class="placeholderColor" />
							</view>
						</view>
						<view class="formItem">
							<view class="label">工资</view>
							<view class="controls">
								<input class="input" type="digit" v-model="formData.salary" name="salary"
									placeholder='可不填写' placeholder-class="placeholderColor" />
							</view>
						</view>
						<view class="formItem">
							<view class="label">加班工资</view>
							<view class="controls">
								<input class="input" type="digit" v-model="formData.overtime_salary" name="overtime_salary"
									placeholder='可不填写' placeholder-class="placeholderColor" />
							</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 底部操作 -->
			<view class="formPost_con">
				<button form-type="submit" hover-class="none" class="submitBtn">提交资料</button>
			</view>
		</form>

		<!-- 上传图片组件 -->
		<Upload v-if="isUpload" @getImgs="getImgsFunc"></Upload>

		<!-- 工种选择 -->
		<u-select v-model="kindshow" mode="mutil-column-auto" :list="kindList" label-name="name" value-name="id"
			child-name="child" @confirm="kindConfirm"></u-select>

	</view>
</template>

<script>
	import Upload from '@/components/upload/upload.vue';
	export default {
		components: {
			Upload
		},
		data() {
			return {
				// 工地
				project_id: '',
				worksiteDetail: {},

				// 工种
				kindshow: false,
				kindList: [],
				kindObj: {
					label: '',
					value: '',
				},

				// 上传图片
				isUpload: false, //是否打开上传图片
				avatarUrl: '',
				avatarId: '',
				
				// 表单
				formData: {
					name: '',
					salary: '',
					mobile: '',
					overtime_salary:'',
				},
			}
		},
		onLoad(option) {
			var _this = this;
			_this.project_id = option.project_id;
		},
		onShow() {

		},
		async onReady() {
			var _this = this;
			await _this.$onLaunched;
			_this.getWorksiteDetail();
			_this.getKindList();
		},
		methods: {
			// 获取工地详情
			getWorksiteDetail() {
				var _this = this;
				_this.$u.get('/api/project.index/getDetail', {
					id: _this.project_id,
				}).then(res => {
					uni.hideLoading();
					console.log("==获取工地详情==");
					console.log(res);
					_this.worksiteDetail = res.data;
				}).catch(res => {
					console.log(res);
				})
			},
			
			// 获取工种列表
			getKindList() {
				var _this = this;
				_this.$u.get('/api/project.job/getTree', {}).then(res => {
					uni.hideLoading();
					console.log("==获取工种列表==");
					console.log(res);
					_this.kindList = res.data;
				}).catch(res => {
					console.log(res);
				})
			},

			// 选择工种
			kindConfirm(e) {
				console.log("==选择工种==");
				console.log(e);
				this.kindObj = e[1];
				this.kindshow = false;
			},

			/**
			 * 上传图片 start
			 */
			// 打开上传图片
			openUpload() {
				this.isUpload = true;
			},

			// 获取上传的图片
			getImgsFunc(e) {
				console.log("==获取上传的图片==");
				console.log(e);
				let _this = this;
				if (e && typeof(e) != 'undefined') {
					_this.avatarUrl = e[0].url;
					_this.avatarId = e[0].imgID;
				}
				_this.isUpload = false;
			},
			/**
			 * 上传图片 end
			 */

			// 提交
			formSubmit(e) {
				// console.log(e);
				var _this = this;
				var formData = e.detail.value;

				if (!formData.name) {
					uni.showModal({
						content: '姓名不能为空',
						showCancel: false,
					})
					return;
				}

				_this.formData = formData;
				_this.$u.throttle(_this.submitForm, 3000);
			},

			// 提交接口
			submitForm() {
				var _this = this;
				_this.$u.post('/api/project.member/addApply', {
					project_id: _this.project_id,
					name: _this.formData.name,
					salary: _this.formData.salary,
					overtime_salary: _this.formData.overtime_salary,
					// mobile: _this.formData.mobile,
					mobile:'',
					work_class: _this.kindObj.label,
					img: _this.avatarId,
					quit: 10,
				}).then(res => {
					uni.showModal({
						content:res.msg,
						showCancel:false,
						success(res){
							uni.reLaunch({
								url: '/pages/index/index'
							})
						}
					})
					
					
				}).catch(res => {
					console.log(res);
				})
			}

		}
	}
</script>

<style>
	page{
		background-color: #fff;
	}
</style>
<style scoped lang="scss">
	.worksiteInfor_box {
		color: #000;
		padding: 20rpx;
		border-bottom: 10rpx solid #eee;
	}
	.form_box{
		padding:0 20rpx;
	}
	.worksiteName {
		font-size: 36rpx;
		font-weight: bold;
	}

	.foremanName {
		font-size: 28rpx;
		margin-top: 10rpx;
	}

	.formTips {
		color: $uni-color-warning;
		padding-top: 30rpx;
	}
	
	.formCon{
		padding-bottom: 30rpx;
	}
	
	.formItem {
		padding: 30rpx 0 0 0;
		border-bottom: 0;
		background-color: #fff;
	}

	.formItem .controls {
		padding: 20rpx;
		border: 1rpx solid #ccc;
		border-radius: 10rpx;
		box-sizing: border-box;
	}

	.picker {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
</style>
